package com.zzq.estimate

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.*
import androidx.appcompat.widget.AppCompatSpinner

class MainActivity : AppCompatActivity() {

    private lateinit var etYang: EditText
    private lateinit var etYanqi: EditText
    private lateinit var tvValue: TextView
    private lateinit var spinner: AppCompatSpinner

    private var type = 9.0f
    private lateinit var typeArray: Array<String>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        typeArray = resources.getStringArray(R.array.boiler_type)
        //氧含量
        etYang = findViewById(R.id.et_yanghanliang)
        //烟气浓度
        etYanqi = findViewById(R.id.et_yanqi)
        //计算结果
        tvValue = findViewById(R.id.tv_zhesuan)

        spinner = findViewById(R.id.spinner)
        val arrayAdapter = ArrayAdapter<String>(
            this,
            android.R.layout.simple_spinner_item,
            typeArray
        )
        spinner.adapter = arrayAdapter
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                type = typeArray[position].toFloat()
                showToast("选择了： $type")
            }

        }
    }

    /**
     * 清屏
     */
    fun clearData(view: View) {
        etYang.setText("")
        etYanqi.setText("")
        spinner.setSelection(0)
        tvValue.setText("")
    }

    /**
     * 计算
     */
    fun calculateData(view: View) {
        //氧含量
        val yang = etYang.text.toString()
        //烟气
        val yanqi = etYanqi.text.toString()

        if (TextUtils.isEmpty(yang)) {
            showToast("未输入氧浓度")
            return
        } else if (TextUtils.isEmpty(yanqi)) {
            showToast("未输入烟气浓度")
            return
        }
        val 氧浓度 = yang.toFloat()
        val 烟气 = yanqi.toFloat()

        val value = ((21 - type) / (21 - 氧浓度)) * 烟气
        tvValue.text = value.toString()
    }

    private fun showToast(msg: String) {
        Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()
    }
}
